package city.spring.modules.material.service.impl;

import city.spring.modules.material.entity.MaterialQuantityEntity;
import city.spring.modules.material.entity.MaterialinventoryMaterialExtEntity;
import city.spring.modules.material.repository.MaterialinventoryMaterialExtRepository;
import city.spring.modules.material.service.MaterialQuantityService;
import city.spring.modules.material.service.MaterialinventoryMaterialExtService;
import city.spring.modules.material.vo.MaterialinventoryMaterialVO;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static city.spring.utils.UserUtils.getPrincipal;

/**
 * Service：物资盘点计划与物资关系表
 *
 * @author 谢金婷
 * @date 2020-07-27 09:44:32
 */
@CacheConfig(cacheNames = {MaterialinventoryMaterialExtServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class MaterialinventoryMaterialExtServiceImpl extends ServiceImpl<MaterialinventoryMaterialExtRepository, MaterialinventoryMaterialExtEntity> implements MaterialinventoryMaterialExtService {

    private final MaterialinventoryMaterialExtRepository materialinventoryMaterialExtRepository;
    private final UserService userService;
    private final MaterialQuantityService materialQuantityService;

    public MaterialinventoryMaterialExtServiceImpl(MaterialinventoryMaterialExtRepository materialinventoryMaterialExtRepository, UserService userService, MaterialQuantityService materialQuantityService) {
        this.materialinventoryMaterialExtRepository = materialinventoryMaterialExtRepository;
        this.userService = userService;
        this.materialQuantityService = materialQuantityService;
    }

    @Override
    public void saveMaterialinventoryMaterialExt(MaterialinventoryMaterialExtEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMaterialinventoryMaterialExt(MaterialinventoryMaterialExtEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMaterialinventoryMaterialExt(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMaterialinventoryMaterialExt(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<MaterialinventoryMaterialExtEntity> getInventoryWarehousingAndDelivery(Pageable pageable) {
        IPage<MaterialinventoryMaterialExtEntity> page = MyBatisUtils.toPage(pageable);
        return materialinventoryMaterialExtRepository.getInventoryWarehousingAndDelivery(page, lambdaQuery().getWrapper());
    }

    @Override
    public IPage<MaterialinventoryMaterialVO> listPage(Page<? extends MaterialinventoryMaterialExtEntity> page, Wrapper<? extends MaterialinventoryMaterialExtEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

    @Override
    public void inventoryCorrection(MaterialinventoryMaterialExtEntity entity) {
        entity.setInventoryWdDate(new Date());
        // 获取当前用户信息
        UserEntity userEntity = userService.getUserByIdOrAccount(getPrincipal().getName());
        entity.setUsersId(userEntity.getId());
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

}